package com.atguigu.ssoserver.refactoring.pojo;


import java.util.Enumeration;
import java.util.Vector;

public class Customer {
    private String _name;
    private Vector<Rental> _rentals = new Vector();

    public String getName(){
        return _name;
    }

    public Customer(String name){
        _name=name;
    }

    //添加租赁的影片
    public void addRental(Rental arg){
        _rentals.addElement(arg);
    }

    //生成租赁明细的函数
    public String statement(){
        Enumeration<Rental> rentals = _rentals.elements();
        String result = "Rental Record for " + getName() + "\n";
        while (rentals.hasMoreElements()) {
            Rental each = rentals.nextElement();

            // show figures for the rental(显示租金的数字)
            result += "\t" + each.getMovie().getTitle() + "\t"
                    + String.valueOf(each.getCharge()) + "\n";
        }
        // add footer lines(添加页脚行)
        result += "Amount owed is " + String.valueOf(getTotalCharge()) + "\n";
        result += "You earned " + String.valueOf(getTotalFrequentRenterPoints())
                + " frequent renter points";
        return result;
    }

    private double getTotalCharge(){
        double result = 0;
        Enumeration<Rental> rentals = _rentals.elements();
        while (rentals.hasMoreElements()){
            Rental each = rentals.nextElement();
            result+=each.getCharge();
        }
        return result;
    }

    private int getTotalFrequentRenterPoints(){
        int result=0;
        Enumeration<Rental> rentals = _rentals.elements();
        while (rentals.hasMoreElements()){
            Rental each = rentals.nextElement();
            result+=each.getFrequentRenterPoints();
        }
        return result;
    }

    public String htmlStatement(){
        Enumeration<Rental> rentals = _rentals.elements();
        String result = "<H1>Rentals for <EM> " + getName() + "</EM></H1>";
        while (rentals.hasMoreElements()) {
            Rental each = rentals.nextElement();

            result += "\t" + each.getMovie().getTitle() + ":"
                    + String.valueOf(each.getCharge()) + "<BR>\n";
        }
        // add footer lines(添加页脚行)
        result += "<P>You owe <EM>" + String.valueOf(getTotalCharge()) + "</EM></P>\n";
        result += "<P>You earned <EM>" + String.valueOf(getTotalFrequentRenterPoints())
                + "</EM> frequent renter points</P>";
        return result;
    }

}
